class FieldPanel extends egret.EventDispatcher {
	private _field:eui.Component
	private _hand:eui.Component
	private _playerNum:0|1

	public places:eui.Component[][]
	
	private _curHighlightHandCard:CardComponent[]
	private _curHighlightPlaces:eui.Component[]
	private _curCard:eui.Component
	
	public iconShield:ActionIcon
	public iconSkill:ActionIcon
	public iconWeapon:ActionIcon
	
	public constructor(pn:0|1, field:eui.Component, hand:eui.Component) {
		super()
		this._field = field
		this._hand = hand	
		this._playerNum = pn
		
		const group0 = (field.getChildAt(0) as eui.Component)
		const group00 = group0.getChildAt(0) as eui.Component
		const group01 = group0.getChildAt(1) as eui.Component
		let group1 = group01.getChildAt(0) as eui.Component
		const sp:eui.Component[] = []
		for(let i=0; i<3; i++){
			sp.push(group1.getChildAt(i) as eui.Component)
		}
		group1 = group01.getChildAt(1) as eui.Component
		const wp:eui.Component[] = []
		for(let i=0; i<3; i++){
			wp.push(group1.getChildAt(i) as eui.Component)
		}

		this.iconShield = group00.getChildAt(0) as ActionIcon
		this.iconSkill = group00.getChildAt(1) as ActionIcon
		this.iconWeapon = group00.getChildAt(2) as ActionIcon
		if(pn == 1){
			group0.setChildIndex(group00, 1)
		}

		this.places = [sp, wp]
		this._curHighlightHandCard = []
		this._curHighlightPlaces = []
	}

	public async moveCard(cmov:CardMoveData){
		const t = this
		if(cmov.toField){
			for(let c of cmov.toField){
				if(c.curPlace && c.tgtPlace){
					//从场上一格移动到另一格
				}
				else if(!c.curPlace && c.tgtPlace){
					//从手牌移动到场上一格
					await c.display.removeFromHand()
					c.display.parent && c.display.parent.removeChild(c.display)
					c.display.x = 0
					c.display.y = 0
					c.display.isThumb = true
					c.display.touchEnabled = false
					t.places[c.tgtPlace.i][c.tgtPlace.j].addChild(c.display)
				}
				c.finishMoving()
			}
		}
		if(cmov.toHand){
			for(let c of cmov.toHand){
				if(c.curPlace && c.tgtPlace){
					//从场上一格移动到手牌
					c.display.parent && c.display.parent.removeChild(c.display)
				}
				await t.addHandCard([c], true)
				c.display.isThumb = true
				c.display.touchEnabled = true
				c.finishMoving()
			}
		}	
	}


	public async addHandCard(cards:BaseCard[], animation:boolean=true){
		const t = this
		for(let c of cards){
			t._hand.addChildAt(c.display, 0)
			c.display.isThumb = true
			c.display.dragged = false
			if(animation){
				await c.display.added2Hand()
			}else{
				await new Promise((resolve, reject)=>{
					setTimeout(resolve, 200)
				})
			}
		}
	}

	public async switchHand(cards:BaseCard[]){
		const t = this
		return new Promise((resolve, reject)=>{
			egret.Tween.get(t._hand.parent)
			.to({
				bottom:-200
			}, 100)
			.call(()=>{
				t._hand.removeChildren()
				for(let c of cards){
					t._hand.addChildAt(c.display, 0)
					c.display.isThumb = true
					c.display.isFront = true
				}
			})
			.to({
				bottom:0
			}, 100)
			.call(resolve)
		})
	}

	public async removeHandCard(cards:BaseCard[], animation:boolean=true){
		const t = this
		let front = true
		for(let c of cards){
			if(animation){
				await c.display.removeFromHand()	
				t._hand.removeChild(c.display)	
			}else{
				await new Promise((resolve, reject)=>{
					setTimeout(resolve, 200)
				})
			}
		}
	}
	
	/**高亮手卡 */
	public highlightHand(cards:BaseCard[]){
		const t = this
		for(let card of t._curHighlightHandCard){
			if(!card)continue
			CardHightLightFilter.instance.removeHightLight(card)
		}
		t._curHighlightHandCard = []
		for(let card of cards){
			CardHightLightFilter.instance.addHighLight(card.display)
			t._curHighlightHandCard.push(card.display)
		}
	}

	/**高亮场地格
	 * @param p2hl 要高亮的格子，
	 */
	public highlightPlace(p2hl:boolean[][]){
		const t = this
		for(let place of t._curHighlightPlaces){
			if(!place)continue
			CardHightLightFilter.instance.removeHightLight(place)
		}
		t._curHighlightPlaces = []
		for(let i=0;i<2;i++){
			for(let j=0;j<3;j++){
				if(p2hl[i][j]){
					const place = t.places[i][j]
					CardHightLightFilter.instance.addHighLight(place)
					t._curHighlightPlaces.push(place)
				}
			}
		}
		return t._curHighlightPlaces
	}

	public hightlightCell(i:number, j:number, v:boolean){
		const t = this
		const place = t.places[i][j]
		// for(let place of t._curHighlightPlaces){
		// 	if(!place)continue
		// 	CardHightLightFilter.instance.removeHightLight(place)
		// }
		if(v){
			CardHightLightFilter.instance.addHighLight(place)
			t._curHighlightPlaces.push(place)
		}else{
			const idx = t._curHighlightPlaces.indexOf(place)
			if(idx >= 0){
				t._curHighlightPlaces.splice(idx,1)
				CardHightLightFilter.instance.removeHightLight(place)
			}
		}
		
	}

	/**取消高亮 */
	public removeAllHighlight(){
		const t = this
		for(let card of t._curHighlightHandCard){
			if(!card)continue
			CardHightLightFilter.instance.removeHightLight(card)
		}
		for(let place of t._curHighlightPlaces){
			if(!place)continue
			CardHightLightFilter.instance.removeHightLight(place)
		}
		t._curHighlightHandCard = []
		t._curHighlightPlaces = []
	}

	public setActIconStatus(p:Player){
		this.iconWeapon.times = p.actWeapon
		this.iconSkill.times = p.actSkill
		this.iconShield.times = p.actShield
	}

}